package com.jeesite.modules.xcxAdmin.service;



import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.utils.HttpRequest;
import com.jeesite.modules.xcxAdmin.entity.SmsInfo;



public class SmsSendService {

	private String debug = "1";
	String uri = "http://api.jisuapi.com/sms/send";
	String appkey="5ccbc706d5e593c5";
	String appsecret="tNtqTsAGprK09WbNunKEguuGAKHbLQ8f";
	
	 public static final String APPKEY = "5ccbc706d5e593c5";// 你的appkey
	    public static final String URL = "http://api.jisuapi.com/sms/send";
	   
	 
	
	 public SmsInfo get(String mobile, String content) {
		 SmsInfo info = new SmsInfo();
	        String result = null;
	        String param="";
			try {
				param = "mobile=" + mobile + "&content=" + URLEncoder.encode(content, "utf-8") + "&appkey="
				        + APPKEY+"&appsecret="+appsecret;
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
	 
	        try {
	           // result = HttpUtil.sendGet(url, "utf-8");
	             result = HttpRequest.sendGet(URL, param);
	            
	             if (result != null && !"".equals(result.trim())
	 					) {
	 				JSONObject jo=new JSONObject();
	 				JSONObject jj=jo.parseObject(result.trim());
	 				if(jj!=null&&jj.getString("status")!=null&&"0".equals(jj.getString("status"))){
	 					info.setRetId(1);
	 				}else{
	 					info.setRetId(-1);
	 				}
	 				
	 			} else {
	 				info.setRetId(-1);
	 			}

	 		} catch (Exception e) {
	 			e.printStackTrace();
	 			info.setRetId(-1);
	 		}
	 		return info;
	       
	    
	}
	
	
	
	public SmsInfo send(String mobiles, String content) {

		SmsInfo info = new SmsInfo();
		if (StringUtils.equals(debug, "0")) {

			info.setRetId(0);
			return info;
		}
		try {

			String param = "appkey="+appkey+"&mobile="
					+ mobiles
					+ "&content="
					+ URLEncoder.encode(content.replaceAll("<br/>", " "),
							"UTF-8");
		
			String result = HttpRequest.sendGet(uri, param);
			
			
			if (result != null && !"".equals(result.trim())
					) {
				JSONObject jo=new JSONObject();
				JSONObject jj=jo.parseObject(result.trim());
				if(jj!=null&&jj.getString("status")!=null&&"0".equals(jj.getString("status"))){
					info.setRetId(1);
				}else{
					info.setRetId(-1);
				}
				
			} else {
				info.setRetId(-1);
			}

		} catch (Exception e) {
			e.printStackTrace();
			info.setRetId(-1);
		}
		return info;
	}
//
	public static void main(String[] args) throws Exception {
		System.out.println(new SmsSendService().send("13480351426", "验证码：1478，如非本人操作，请忽略本短信【优生活】"));
	}
}